<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-form :inline="true" ref="searchForm" :model="searchObj" :rules="searchRules" label-width="80px">
        <el-form-item label="版本类型" prop="value">
          <el-select v-model="searchObj.value" placeholder="请选择">
            <el-option
              v-for="item in versionTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否为强制升级" prop="forceUpgrade">
          <el-select v-model="searchObj.forceUpgrade" placeholder="请选择">
            <el-option
              v-for="item in upgradeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-select v-model="searchObj.userType" placeholder="请选择" >
            <el-option
              v-for="item in userOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="版本名称" prop="versionName">
          <el-input placeholder="请输入" v-model="searchObj.versionName">
          </el-input>
        </el-form-item>
        <el-form-item label="版本编号" prop="versionSn">
          <el-input placeholder="请输入" v-model="searchObj.versionSn">
          </el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary">搜索</el-button>
          <el-button type="primary" @click="doReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row class="mt-20 pl-10" type="flex" justify="start" :gutter="20">
      <el-button type="success" @click="showDialog('新增')">新增</el-button>
      <el-button type="danger" :disabled="btnDisabled" @click="showComfirm">批量删除</el-button>
    </el-row>
    <el-table class="mt-20" :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="title" label="标题">
      </el-table-column>
      <el-table-column prop="publisher" label="发布人">
      </el-table-column>
      <el-table-column prop="publishTime" label="发布时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showDownload(scope.row.id)">下载</el-button>
          <!-- <el-button type="text" size="small" @click="showDetailDialog(scope.row)">查看</el-button>
          <el-button type="text" size="small" @click="showDialog('编辑',scope.row)">编辑</el-button> -->
          <el-button type="text" size="small" @click="showComfirm(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="mt-20" type="flex" justify="center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-row>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="APP类型" prop="value">
          <el-select v-model="form.value" placeholder="请选择">
            <el-option
              v-for="item in versionTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="版本类型" prop="value">
          <el-select v-model="form.value" placeholder="请选择">
            <el-option
              v-for="item in versionTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="版本名称" prop="versionName">
          <el-input v-model="form.versionName"></el-input>
        </el-form-item>
        <el-form-item label="版本号" prop="versionSn">
          <el-input v-model="form.versionSn"></el-input>
        </el-form-item>
        <el-form-item label="版本文件" >
          <el-upload
            class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" :on-change="handleChange">
            <el-button size="small" type="primary">点击上传</el-button>
            <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
          </el-upload>
        </el-form-item>
        <el-form-item label="版本描述" prop="versionDescription">
          <el-input type="textarea":rows="2" placeholder="请输入内容" v-model="form.versionDescription"></el-input>
        </el-form-item>
        <el-form-item label="是否强制升级" prop="forceUpgrade">
          <el-radio v-model="form.forceUpgrade" label="1">是</el-radio>
          <el-radio v-model="form.forceUpgrade" label="2">否</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEdit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="detailDialogTitle"
      :visible.sync="detailDialogVisible"
      width="50%"
      :before-close="handleDetailClose">
      <el-form :model="detailForm" label-width="80px">
        <el-form-item label="id:" prop="title">
          {{detailForm.id}}
        </el-form-item>
        <el-form-item label="标题:" prop="title">
          {{detailForm.title}}
        </el-form-item>
        <!-- <el-form-item label="内容" prop="content">
          <el-input type="textarea" :rows="2" v-model="detailForm.content"></el-input>
        </el-form-item> -->
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { getList } from "@/api/table";
import { propertyInfoTableData } from "@/constants/mockDatas";
import { deleteConfirm } from "@/utils/deleteConfirm";
import { requireInput,requireInputWithMaxLengthN } from "@/utils/validateForm";
import { versionTypeList,userOptions,upgradeOptions} from "@/constants/code";

export default {
  data() {
    const validateDate = (rule, value, callback) => {
      let num =
        new Date(this.searchObj.startTime).getTime() -
        new Date(this.searchObj.endTime).getTime();
      if (num > 0) {
        callback(new Error("结束时间不能超过开始时间"));
      } else if (value == "") {
        callback(new Error("请选择结束时间"));
      } else {
        callback();
      }
    };

    return {
      searchObj: {
        value: "全部",
        userType: "全部",
        value: "全部",
        forceUpgrade:"全部",
        versionName: "",
        versionSn:""
      },
      searchRules: {
        versionName: requireInputWithMaxLengthN(),
        versionSn: requireInputWithMaxLengthN(),
      },
      list: propertyInfoTableData.data.rows,
      listLoading: false,
      currentPage: 1,
      total: 10,
      pageSize: 5,
      pageSizes: [5, 10],
      btnDisabled: true,
      multipleSelection: [],
      dialogVisible: false,
      detailDialogVisible:false,
      dialogTitle: "添加版本",
      detailDialogTitle:"详情",
      form: {
        appType:"",
        versionType:"",
        versionName:"",
        versionSn:"",
        versionFile:"",
        versionDescription: "",
        forceUpgrade: "2"
      },
      detailForm:{

      },
      rules: {
        appType:requireInput("请输入app类型"),
        versionType:requireInput("请输入版本类型"),
        versionName:requireInput("请输入版本名称"),
        versionSn:requireInput("请输入版本编号"),
        versionFile:requireInput("请添加版本文件"),
        versionDescription: requireInput("请输入版本描述")
      },
      versionTypeList,
      userOptions,
      upgradeOptions,
      fileList3:[]
    };
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  created() {},
  methods: {
    doReset() {
      this.$refs.searchForm.resetFields(); //对整个表单进行重置
    },

    showDialog(type, row = false) {
      this.dialogTitle = type;
      if (row) {
        this.form.title = row.title;
        this.form.content = row.content;
      }
      this.dialogVisible = true;
    },

    showDetailDialog(scope){
      this.detailForm = scope
      this.detailDialogVisible = true
    },

    addOrEdit() {
      if (this.dialogTitle == "添加版本") {
        //this.toinsertAsync()
      } else {
        //this.toupdateAsync()
      }

      this.dialogVisible = false;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = {
        title: "",
        content: ""
      };
    },

    handleDetailClose(){
      this.detailDialogVisible = false
    },

    doDelete(id = null) {
      let ids = id || this.multipleSelection.map(item => item.id);
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },

    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },

    showComfirm(id = null) {
      deleteConfirm(this, this.doDelete);
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.btnDisabled = this.multipleSelection.length < 1;
    },
    handleChange(file, fileList) {
        this.fileList3 = fileList.slice(-3);
    },
    showDownload(id) {
        const h = this.$createElement;
        this.$msgbox({
          title: '确认信息',
          message: h('p', null, [
            h('span', null, '您确定要下载该版本吗?'),
            h('i', { style: 'color: teal' }, 'id:'+id)
          ]),
          showCancelButton: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = '执行中...';
              setTimeout(() => {
                done();
                setTimeout(() => {
                  instance.confirmButtonLoading = false;
                }, 300);
              }, 3000);
            } else {
              done();
            }
          }
        }).then(action => {
          this.$message({
            type: 'info',
            message: 'action: ' + action
          });
        });
      }
  }
};
</script>
